import { BaseCommand, TSelectRanage } from '../types/common.interface';
import { TableDatas } from '../cors/table-datas';
import { RenderTrigger } from '../cors/render-trigger';
import { NFill } from '../types/style.interface';
import { clone } from '../utils/deep-clone';
import { FullControl } from '../cors/full-control';
export class FillCommand extends BaseCommand {
    protected pastFill: {row: number, col: number, fill: Partial<NFill.IFill>}[] = [];
    constructor(
        protected datas: TableDatas, 
        protected range: TSelectRanage, 
        protected renderTrigger: RenderTrigger,
        protected fullCtl: FullControl,
        protected fill: {background: {color: string}}
    ) {
        super();
    }
    protected fillNormalCells() {
        for(let i = this.range.row; i < this.range.row + this.range.rowspan; i++) {
            let rowInstance = this.datas.getRow(i);
            for(let j = this.range.col; j < this.range.col + this.range.colspan; j++) {
                let merge = this.datas.mergeCell.getMerge(i,j);
                if(merge != null && (merge.row != i || merge.col != j)) {
                    continue;
                }
                let cellInstance = rowInstance.getCell(j);
                this.pastFill.push({
                    row: i,
                    col: j,
                    fill: clone(cellInstance.fill)
                });
                cellInstance.fill = this.fill;
                rowInstance.setCell(j, cellInstance);
                this.datas.setRow(rowInstance, i);
            }
        }
        this.renderTrigger.triggerRenderAll();
    }
    protected fillFullRowAndCol() {
        if(this.range.row === -1) {
            for(let i = this.range.col; i < this.range.col + this.range.colspan; i++) {
                let cells = this.datas.getFullCols(i);
                for(let cell of cells) {
                    cell.cell.fill =  null;
                }
                this.fullCtl.setColStyle(i, {fill: this.fill});
            }
        }
        if(this.range.col === -1) {
            for(let i = this.range.row; i < this.range.row + this.range.rowspan; i++) {
                let cells = this.datas.getFullRows(i);
                for(let cell of cells) {
                    cell.cell.fill = null;
                }
                this.fullCtl.setRowStyle(i, {fill: this.fill});
            }
        }
        this.renderTrigger.triggerRenderAll();
    }
    public execute() {
        if(this.range.col === -1 || this.range.row === -1) {
            this.fillFullRowAndCol();
        } else {
            this.fillNormalCells();
        }
        return true;
    }
    public redo() {
        for(let item of this.pastFill) {
            let rowInstance = this.datas.getRow(item.row);
            let cellInstance = rowInstance.getCell(item.col);
            cellInstance.fill = item.fill;
            rowInstance.setCell(item.col, cellInstance);
            this.datas.setRow(rowInstance, item.row);
        }
        this.renderTrigger.triggerRenderAll();
        return true;
    }
}